package cn.com.seederland.gkrpc.client;

import cn.com.seederland.gkrpc.codec.Decoder;
import cn.com.seederland.gkrpc.codec.Encoder;
import cn.com.seederland.gkrpc.common.utils.ReflectionUtils;

import java.lang.reflect.Proxy;

public class RpcClient {

    private RpcClientConfig config;

    private Encoder encoder;

    private Decoder decoder;

    private TransportSelector selector;

    public RpcClient() {
        this(new RpcClientConfig());
    }

    public RpcClient(RpcClientConfig config) {
        this.config = config;
        this.encoder = ReflectionUtils.newInstance(this.config.getEncoderClass());
        this.decoder = ReflectionUtils.newInstance(this.config.getDecoderClass());
        this.selector = ReflectionUtils.newInstance(this.config.getTransportSelectorClass());

        this.selector.init(this.config.getServers(), this.config.getConnectCount(), this.config.getTransportClass());


    }

    public <T> T getProxy(Class<T> clazz) {
        return (T)Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{clazz}, new RemoteInvoke(clazz, encoder, decoder, selector));
    }

}
